package db

import (
	"errors"
	"log"
	"server_cluster/battle/sharemem"
	"strconv"
	"strings"
)

type Config_passive struct {
	Id      int32  `gorm:"primary_key;column:id;autoIncrement:false" csv:"Id"`
	Cd_time int32  `gorm:"column:cd_time" csv:"CdTime"`
	Type    int8   `gorm:"column:Type" csv:"Type"`
	Skill   string `gorm:"column:skill" csv:"PassiveSkill"`
}

func (tmp *Config_passive) TableName() string {
	return "config_passive"
}

func (tmp *Config_passive) getAll() ([]*Config_passive, error) {
	var data []*Config_passive
	result := Conn.Find(&data)

	return data, result.Error
}

func (tmp *Config_passive) LoadData() error {
	data, err := tmp.getAll()
	if err != nil {
		return err
	}
	var myshm = &(sharemem.Myshm.Cfg.Sklpas)
	for key, value := range data {
		if key >= sharemem.MONSTER_MAX {
			return errors.New("config_passive format error 8")
		}
		myshm[key].Id = value.Id
		myshm[key].Cd_time = value.Cd_time
		myshm[key].Type = value.Type
		if len(value.Skill) <= 0 {
			return errors.New("config_passive format error 1")
		}
		b_list := strings.Split(value.Skill, ";")
		if len(b_list) > sharemem.CARD_SKILL_MAX {
			return errors.New("config_passive format error 2")
		}
		for k, v := range b_list {
			arg, err := strconv.Atoi(v)
			if err != nil {
				return errors.New("config_passive format error 22")
			}
			myshm[key].Skill[k] = int32(arg)
		}
	}

	log.Println("load config_passive completed.")

	return nil
}
